﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KinectDAL;
using System.IO;
using Kinect_Repository.net.cloudapp.logcollectorazure;
using Kinect_Repository.net.cloudapp.logcollectorazure1;
using NLog;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using System.Diagnostics;

namespace Kinect_Repository
{
    public class PicturesApplicationViewModel : NotificationObject
    {
        public DelegateCommand<string> OpenCommand { get; set; }
        public PicturesApplicationViewModel()
            : base()
        {
            this.selectedDate = DateTime.Now;
            OpenCommand = new DelegateCommand<string>(OpenFile);
        }
        void OpenFile(string pathFile)
        {
            Process.Start(pathFile);
        }
        static Logger log = LogManager.GetCurrentClassLogger();
        
        public List<I_UserFile> filesOnDate ;
         public List<I_UserFile> FilesOnDate
        {
            get
            {
                return filesOnDate;
            }
            set
            {
                filesOnDate= value;
                this.RaisePropertyChanged<List<I_UserFile>>(() => this.FilesOnDate);
                if (OpenCommand != null)
                    OpenCommand.RaiseCanExecuteChanged();
                
            }
        }

       
        private DateTime selectedDate;
        public DateTime SelectedDate
        {
            get
            {
                return selectedDate;
            }
            set
            {
                selectedDate= value;
                LoadFiles(value);
                this.RaisePropertyChanged<DateTime>(() => this.SelectedDate);
                this.RaisePropertyChanged<List<I_UserFile>>(() => this.FilesOnDate);
                
            }
        }

        private void LoadFiles(DateTime? dt)
        {
            try
            {
                PicturesApplication p=new PicturesApplication();

                FilesOnDate = p.Files(dt.Value, SettingsApplication.NameConnectionDefault);
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled)
                    log.ErrorException("Load files", ex);
            }
        }

    }
    public class PicturesApplication 
    {

        

        static Logger log = LogManager.GetCurrentClassLogger();
        public List<I_UserFile> Files(DateTime fromDate,string ConnectionName)
        {
            using (UserFile_List ul = new UserFile_List(ConnectionName))
            {
                ul.LoadFromDB.LoadFromCreatedDateExactly(fromDate);
                return ul.ToList<I_UserFile>();
            }            
        }
        private bool StoreFileInCLoud(byte[] fileBytes, string ConnectionName, SettingsApplication app)
        {
            if (app.IsUserRegistered)
            {
                try
                {
                    LogData ld = new LogData();
                    var guidlog = ld.LogFast(app.ApplicationGuid, "new picture from " + Environment.MachineName);
                    LogPicture lp = new LogPicture();
                    lp.LogData(guidlog, app.ApplicationGuid, fileBytes);
                    return true;
                }
                catch (Exception ex)
                {
                    if (log.IsErrorEnabled) 
                    { 
                        log.Error("saving to cloud", ex);
                    }
                    return false;
                }
            }
            return false;
        }
        
        public void StoreFile(byte[] fileBytes, string ConnectionName, SettingsApplication app)
        {

            DateTime dtNow=DateTime.Now;
            string NameFile=string.Format("kinect_{0:yyyyMMdd_HHmmss}.jpg",dtNow);

            string pathFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"Pictures");
            if(!Directory.Exists(pathFile)){
                Directory.CreateDirectory(pathFile);
            }
            pathFile = Path.Combine(pathFile,NameFile);
            File.WriteAllBytes(pathFile,fileBytes);

            bool savedToCloud = StoreFileInCLoud(fileBytes,ConnectionName,app);
            

            using (UserFile_List ul = new UserFile_List(ConnectionName))
            {
                ul.Add(new UserFile() { CreatedDate = dtNow, NameFile = NameFile, PathFile = pathFile, SavedInCloud = savedToCloud });
                ul.SaveNew();
                
            }
        }
    }
}
